﻿using System;
using System.Data;
using System.IO;
using PBO.Data.SQLite.DAL;
using PBO.Data.Util;
using PBO.Sql;

namespace PBO.Data.SQLite
{
    public static class SQLiteHelper
    {
        public readonly static DbHelperSQLite DbHelperSQLite;
        private const string DB_NAME = "data.db";
        private const string DB_PWD = "";

        //数据库连接字符串(web.config来配置)，可以动态更改connectionString支持多数据库.
        public static string ConnectionString
        {
            set
            {
                DbHelperSQLite.ConnectionString = value;
            }
        }

        static SQLiteHelper()
        {
            string dir = AppDomain.CurrentDomain.BaseDirectory;
            string path = Path.Combine(dir, DB_NAME);
            string str = BuildConnectionString(path, DB_PWD);
            DbHelperSQLite = new DbHelperSQLite(str);
        }

        public static string BuildConnectionString(string path, string pwd)
        {
            string conStr = "Data Source=" + path;
            if (!string.IsNullOrEmpty(pwd))
            {
                conStr += ";Password=" + pwd;
            }
            return conStr;
        }

        public static DALMoveData MoveData = DALMoveData.GetInstance();
        public static DALMoveLearnData MoveLearnData = DALMoveLearnData.GetInstance();
        public static DALPokemonData Pokemon = DALPokemonData.GetInstance();
        public static DALItemData Item = DALItemData.GetInstance();
        public static DALAbility Ability = DALAbility.GetInstance();

        #region message

        public static string GetMsg(int id)
        {
            string sql = "select value from pbo_msg where id=" + id;
            var dt = DbHelperSQLite.ExecuteDataTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt.Rows[0][0].ToString();
            }
            return string.Empty;
        }

        public static string GetMsg(string key)
        {
            string sql = "select value from pbo_msg where key like '" + key + "'";
            var dt = DbHelperSQLite.ExecuteDataTable(sql, 1, 1);
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt.Rows[0][0].ToString();
            }
            return string.Empty;
        }

        public static int GetMsgArgb(string key)
        {
            string sql = "select argb from pbo_msg where key like '" + key + "'";
            var dt = DbHelperSQLite.ExecuteDataTable(sql, 1, 1);
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt.Rows[0][0].ToString().ToInt();
            }
            return -65536;
        }

        #endregion

        public static DataTable GetTable(string sql)
        {
            return DbHelperSQLite.ExecuteDataTable(sql);
        }

        public static object GetSingle(string sql)
        {
            return DbHelperSQLite.GetSingle(sql);
        }
    }
}